package com.csi.controller.back;

import cn.hutool.db.sql.Order;
import cn.hutool.jwt.JWTUtil;
import com.csi.domain.Result;
import com.csi.domain.UserBook;
import com.csi.service.UserBookService;
import com.csi.utils.ReturnResults;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/userBook")
public class UserBookController {

    @Autowired
    private UserBookService userBookService;

    @RequestMapping("/showUserBook")
    public Object showUserBook(HttpServletRequest request){
        String token = request.getHeader("token");
        long hotelId = Long.parseLong(JWTUtil.parseToken(token).getPayload("hotelId").toString());
        Map<String,Object> userBooks = userBookService.showUserBook(hotelId);
        if (userBooks != null || userBooks.size()>0){
            return new Result("查询成功", ReturnResults.ACCESS_SUCCESS,userBooks.values());
        }
        return new Result("查询失败",ReturnResults.ACCESS_FAILED);
    }
    /**
     * 根据入住日、离店日、房型模糊查询可订几间房
     * @param roomId
     * @param check_time
     * @param leave_time
     * @return
     */
    @GetMapping("/findRoomNum")
    public Object findRoomNum(@RequestParam("roomId") int roomId,
                           @RequestParam("check_time") String check_time,
                           @RequestParam("leave_time") String leave_time) {

        int result = userBookService.findRoomNum(roomId, check_time, leave_time);
            return new Result("查询成功", ReturnResults.ACCESS_SUCCESS,result);
    }

    /**
     * 根据房型Id查询预定页面酒店及房型信息
     * @param roomTypeId
     * @return
     */
    @GetMapping("/findByRoomTypeId4Info")
    public Object findByRoomTypeId4Info(@RequestParam("roomTypeId") int roomTypeId) {
        Map<String,Object> map = userBookService.findByRoomTypeId4Info(roomTypeId);
        if (map != null) {
            return new Result("查询成功",ReturnResults.ACCESS_SUCCESS,map);
        }
        return new Result("查询失败",ReturnResults.ACCESS_FAILED);
    }

    /**
     * 预定酒店房间
     * @param request
     * @param checkTime
     * @param leaveTime
     * @param roomNum
     * @param bookPrice
     * @param phone
     * @param hotelId
     * @param roomtypeId
     * @return
     */
    @PostMapping("/saveBook")
    public Object saveBook(HttpServletRequest request,
                           @RequestParam("check_time") String checkTime,
                           @RequestParam("leave_time") String leaveTime,
                           @RequestParam("room_num") int roomNum,
                           @RequestParam("book_price") double bookPrice,
                           @RequestParam("phone") String phone,
                           @RequestParam("hotelId") long hotelId,
                           @RequestParam("roomtypeId") long roomtypeId) throws ParseException {

        String token = request.getHeader("token");
        long userId = Long.parseLong(JWTUtil.parseToken(token).getPayload("userId").toString());

        int result = userBookService.saveBook(userId,checkTime,leaveTime,roomNum,bookPrice,phone,hotelId,roomtypeId);
        if (result > 0) {
            return new Result("预定成功", ReturnResults.ACCESS_SUCCESS,result);
        }
        return new Result("预定失败",ReturnResults.ACCESS_FAILED);
    }

    /**
     * 用户支付
     * @param userBook
     * @return
     */
    @PostMapping(value="payment",produces = "application/json;charset=utf-8")
    public Object payment(HttpServletRequest request,@RequestBody UserBook userBook) throws ParseException {
        String token = request.getHeader("token");
        long userId = Long.parseLong(JWTUtil.parseToken(token).getPayload("userId").toString());

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(String.valueOf(userBook.getCheckTime()));
        Timestamp check = new Timestamp(date.getTime());

        Date date1 = sdf.parse(String.valueOf(userBook.getLeaveTime()));
        Timestamp leave = new Timestamp(date1.getTime());

        userBook.setCheckTime(check);
        userBook.setLeaveTime(leave);


        userBook.setUserId(userId);
        int result = userBookService.payment(userBook);
        if (result > 0) {
            return new Result("支付成功", ReturnResults.ACCESS_SUCCESS,result);
        }
        return new Result("支付失败",ReturnResults.ACCESS_FAILED);
    }

    /**
     * 已完成
     * @param bookId
     * @return
     * @throws ParseException
     */
    @RequestMapping("/completed")
    public Object completed(@RequestBody UserBook userBook) throws ParseException {
        long bookId = userBook.getId();
        int result = userBookService.completed(bookId);
        if (result > 0) {
            return new Result("修改成功", ReturnResults.ACCESS_SUCCESS,result);
        }
        return new Result("修改失败",ReturnResults.ACCESS_FAILED);
    }

    /**
     * 待评价
     * @param bookId
     * @return
     */
    @RequestMapping("/evaluated")
    public Object evaluated(@RequestBody UserBook userBook) {
        long bookId = userBook.getId();
        int result = userBookService.evaluated(bookId);
        if (result > 0) {
            return new Result("修改成功",ReturnResults.ACCESS_SUCCESS,result);
        }
        return new Result("修改失败",ReturnResults.ACCESS_FAILED);
    }
}
